﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BusinessLayer;


namespace WebApplication.Controllers
{
    [Authorize]
    public class CarsController : ApiController
    {
        
        public List<Dictionary<string, object>> GetCars()
        {
            List<Dictionary<string, object>> cars = new List<Dictionary<string, object>>();
            Dictionary<string, object> car;

            Reader<Car> reader = Car.Repository.ReadAll();
            while (reader.Read())
            {
                car = new Dictionary<string, object>();
                car["name"] = reader.Current.Name;
                car["x"] = reader.Current.CurrentState().Location.X;
                car["y"] = reader.Current.CurrentState().Location.Y;
                car["directionSin"] = Math.Sin(reader.Current.CurrentState().Direction);
                car["directionCos"] = Math.Cos(reader.Current.CurrentState().Direction);
                car["speed"] = reader.Current.CurrentState().Speed;
                car["oilPressure"] = reader.Current.CurrentState().OilPressure;
                car["gasPressure"] = reader.Current.CurrentState().GasPressure;
                car["time"] = reader.Current.CurrentState().Time;
                car["driverName"] = reader.Current.CurrentDriver().Name;
                car["driverSurname"] = reader.Current.CurrentDriver().Surname;
                car["id"] = reader.Current.Id;

                cars.Add(car);
            }
            reader.Close();

            return cars;
        }

        public Dictionary<string, object> GetCarById(int id)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            Car car = Car.Repository.ReadById(id);
            result["name"] = car.Name;
            result["driver"] = car.CurrentDriver().Name + ' ' + car.CurrentDriver().Surname;
            result["phoneNumber"] = car.CurrentDriver().PhoneNumber;
            result["email"] = car.CurrentDriver().Email;

            return result;
        }
    }
}
